#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import annotations

from typing import Any

from cache3 import Cache

from autoapi.common.log import log


class VariableCache:
    def __init__(self, cache: Cache = Cache("autoapi_cache_vars")) -> None:
        self.cache = cache

    def get(self, key: str, **kwargs) -> Any:
        result = self.cache.get(key, **kwargs)
        if result:
            log.info(f"🔍 获取变量: {key} = {result}")
        return result

    def set(self, key: Any, value: Any, **kwargs) -> bool:
        result = self.cache.set(key, value, **kwargs)
        if result:
            log.info(f"   🔹 储存变量: {key} = {value}")
            log.info(" ✅️ 变量已储存成功")
        return result

    def delete(self, key: str, **kwargs) -> bool:
        result = self.cache.delete(key, **kwargs)
        if result:
            log.info(f"🧹 删除变量: {key}")
        return result

    def has_key(self, key: str, **kwargs) -> bool:
        """
        是否存在缓存值

        :param key:
        :param kwargs:
        :return:
        """
        result = self.cache.has_key(key, **kwargs)
        if result:
            log.info(f"存在临时变量: {key}")
        return result

    def incr(self, key: str, **kwargs) -> int | float:
        """
        是否存在缓存值, 当不存在时触发 ValueError

        :param key:
        :param kwargs:
        :return:
        """
        result = self.cache.incr(key, **kwargs)
        if result:
            log.info(f"存在临时变量: {key}")
        return result

    def clear(self) -> bool:
        result = self.cache.clear()
        if result:
            log.info("🧹 清空临时变量")
        return result


variable_cache = VariableCache()
